package com.wanxi.education.controller.back;

import com.github.pagehelper.PageInfo;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.agency.*;
import com.wanxi.education.service.AgencyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: WangRui
 * @Description:
 * @Date: Created in 2021/9/24 0024
 * @Version: 1.0
 */
@RestController("AgencyBackController")
@RequestMapping("/back/agency")
@Api(tags = "合作机构管理 （后台管理）")
@Slf4j
public class AgencyController {

    @Autowired
    private AgencyService agencyService;

    @ApiOperation("添加机构信息")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody AgencyAddVo agencyAddVo) {
        if (StringUtils.isBlank(agencyAddVo.getName())) {
            return CommonResult.failure("机构名称为空");
        }
        if (StringUtils.isBlank(agencyAddVo.getAddress())) {
            return CommonResult.failure("机构地址为空");
        }
        Long i = agencyService.add(agencyAddVo);
        return CommonResult.success("成功");
    }

    @ApiOperation("通过id，查询机构信息")
    @GetMapping("/findById")
    public CommonResult<AgencyVo> findById(@RequestParam("id") Long id) {
        if (ObjectUtils.isEmpty(id)) {
            return CommonResult.failure("机构id不能为空");
        }
        AgencyVo agencyVo = agencyService.findById(id);
        return CommonResult.success(agencyVo);
    }

    @ApiOperation("分页查询找到所有机构信息，包括模糊查询")
    @PostMapping("/findPage")
    public CommonResult<PageInfo<AgencyVo>> findPage(@RequestBody AgencyPageVo agencyPageVo) {
        PageInfo<AgencyVo> pageInfo = agencyService.findPage(agencyPageVo);
        return CommonResult.success(pageInfo);
    }

    @ApiOperation("通过id ，更新机构信息")
    @PostMapping("/updateById")
    public CommonResult<String> updateById(@RequestBody AgencyUpdateVo agencyUpdateVo) {
        if (ObjectUtils.isEmpty(agencyUpdateVo.getId())){
                return CommonResult.failure("机构id不能为空");
        }
        if (StringUtils.isBlank(agencyUpdateVo.getName())){
                return CommonResult.failure("机构名称不能为空");
        }
        if (StringUtils.isBlank(agencyUpdateVo.getAddress())){
                return CommonResult.failure("机构地址不能为空");
        }
        
        agencyService.updateById(agencyUpdateVo);
        return CommonResult.success("成功");
    }

    @ApiOperation("通过id，删除机构")
    @GetMapping("/deleteById")
    public CommonResult<String> deleteById(@RequestParam("id") Long id) {
        if (ObjectUtils.isEmpty(id)){
                return CommonResult.failure("机构id不能为空");
        }
        agencyService.deleteById(id);
        return CommonResult.success("成功");
    }

    @ApiOperation("查询所有机构信息")
    @GetMapping("/findInfoAll")
    public CommonResult<List<AgencyInfoVo>> findInfoAll(){
        List<AgencyInfoVo> list = agencyService.findInfoAll();
        return CommonResult.success(list);
    }

    @ApiOperation("查询未绑定机构信息")
    @GetMapping("/findInfoAllNotBinding")
    public CommonResult<List<AgencyInfoVo>> findInfoAllNotBinding(){
        List<AgencyInfoVo> list = agencyService.findInfoAllNotBinding();
        return CommonResult.success(list);
    }
}
